<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>商品详情</title>
  <script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <!-- 引入 axios -->
  <script src="config.js" type="module"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 20px;
    }
    .product-detail {
      max-width: 800px;
      margin: 0 auto;
    }
    .product-image {
      width: 100%;
      max-height: 400px;
      object-fit: cover;
    }
    .product-info {
      margin-top: 20px;
    }
    .product-info h1 {
      margin-bottom: 10px;
    }
    .product-info p {
      margin: 8px 0;
    }
    .action-buttons {
      margin-top: 20px;
    }
    button {
      padding: 10px 20px;
      margin-right: 10px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      color: white;
    }
    .add-to-cart {
      background-color: #1890ff;
    }
    .like-btn {
      background-color: #f5222d;
    }
  </style>
</head>
<body>
  <div id="product-detail" class="product-detail">
    <img class="product-image" :src="product.imageUrl" alt="">
    <div class="product-info">
      <h1>{{ product.name }}</h1>
      <p>价格：￥{{ product.price }}</p>
      <p>库存：{{ product.stock }}</p>
      <p>描述：{{ product.description }}</p>
      <div class="action-buttons">
        <button class="add-to-cart" @click="addToCart(product.productId)">加入购物车</button>
        <button class="like-btn" @click="toggleLike(product.productId)">
          {{ isLiked ? '取消收藏' : '收藏' }}
        </button>
      </div>
    </div>
  </div>

</body>
<script>
  new Vue({
    el: '#product-detail',
    data() {
      return {
        product: null
      }
    },
    methods: {
      // 获取商品详情
      async getProductDetail(productId) {
        try {
          const response = await axiosInstance.get(`/product/detail?productId=${productId}`);
          this.product = response.data;
        } catch (error) {
          console.error('获取商品详情失败:', error);
        }
      },
      // 加入购物车
      async addToCart(productId) {
        try {
          await axiosInstance.post(`/cart/add?productId=${productId}`);
          alert('已加入购物车');
        } catch (error) {
          console.error('加入购物车失败:', error);
        }
      }
    },
    mounted() {
      const productId = new URLSearchParams(window.location.search).get('productId');
      if (productId) {
        this.getProductDetail(productId);
      }
    }
  });
</script>